package com.example.administrator.myapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.support.v4.text.TextUtilsCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    private Button btn_dl;
    private String userName,psw,spPsw;
    private EditText et_user_name,et_psw;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        init();
    }
    private void init(){
        et_user_name=(EditText) findViewById(R.id.et_user_name);
        et_psw=(EditText) findViewById(R.id.et_psw);
        btn_dl=(Button) findViewById(R.id.btn_dl);
        //登录按钮点击事件
        btn_dl.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                userName=et_user_name.getText().toString().trim();
                psw=et_psw.getText().toString().trim();
                String md5Psw= MD5Utils.md5(psw);
                spPsw=readPsw(userName);
                if (TextUtils.isEmpty(userName)) {
                    Toast.makeText(LoginActivitythis, "请输入用户名",
                            Toast.LENGTH_SHORT).show();
                    return;
                }
                else if (TextUtils.isEmpty(psw)){
                    Toast.makeText(MainActivity.this, "请输入密码",
                            Toast.LENGTH_SHORT).show();
                    return;
                }else if (md5Psw.equals(spPsw)){
                    Toast.makeText(MainActivity.this,"登录成功",
                            Toast.LENGTH_SHORT).show();
                    saveLoginStatus(true,userName);
                   Intent data=new Intent();
                    data.putExtra("isLogin",true);
                    setResult(RESULT_OK,data);
                    MainActivity.this.finish();
                    return;

                }else if((!TextUtils.isEmpty(spPsw)&&!
                md5Psw.equals(spPsw))){
                    Toast.makeText(MainActivity.this,"输入的用户名和密码不一致",
                            Toast.LENGTH_SHORT).show();
                    return;

                }else{
                    Toast.makeText(MainActivity.this,"此用户不存在",
                            Toast.LENGTH_SHORT).show();
                }

            }

        });

    }
    private String readPsw(String userName){
        SharedPreferences sp = getSharedPreferences("loginInfo",
                MODE_PRIVATE);
        return sp.getString(userName,"");

    }
    private void saveLoginStatus(boolean status,String userName){
        SharedPreferences sp =getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putBoolean("isLogin",status);
        editor.putString("loginUserName",userName);
        editor.commit();
    }
}
